package com.lbs.until;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author Administrator
 * @date 2021/5/17 9:11
 * @description
 **/
public class JdbcUntil {

    private static JdbcUntil jdbcUntil;

    private static DataSource dataSource;

    public JdbcUntil() throws Exception {
        Properties properties = new Properties();
        InputStream stream = JdbcUntil.class.getResourceAsStream("druid.properties");
        properties.load(stream);
        dataSource = DruidDataSourceFactory.createDataSource(properties);
    }

    public synchronized static JdbcUntil getInstance() {

        try {
            if (jdbcUntil == null) {
                jdbcUntil = new JdbcUntil();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jdbcUntil;
    }

    public Connection getConnection(){
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    public void CloseAll(Connection connection){
        if (connection != null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}
